import json
import glob

def load_json(filename):
    with open(filename, 'r') as f:
        return json.load(f)

def merge_json(files):
    merged_data = {"groups": []}
    for file in files:
        data = load_json(file)
        merged_data["groups"].append(data)
    return merged_data

def save_json(data, filename):
    with open(filename, 'w') as f:
        json.dump(data, f, indent=4)

files = glob.glob("../result/group_*.json")
if files:
    files.sort(key=lambda x: int(x.split("_")[-1].split(".")[0]))
    merged_data = merge_json(files)

    save_json(merged_data, "../result/group_merge.json")

    print("JSON files merged successfully into 'merged_groups.json'")
